﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.CacheData
{
    public static class CacheDataUtil
    {
        static long s_timeoutTicks = TimeSpan.FromSeconds(20).Ticks;

        /// <summary>
        /// 更新用户的最新活跃时间
        /// </summary>
        /// <param name="serverData"></param>
        /// <param name="userIdentity"></param>
        public static void UpdateClientLatestActiveTicks(ServerData serverData, string userIdentity)
        {
            if (!string.IsNullOrEmpty(userIdentity))
            {
                Client client = serverData.GetClient(userIdentity);
                if (client != null)
                {
                    client.LatestActiveTicks = DateTime.Now.Ticks;

                    if (client.Callback == null)
                    {
                        client.Callback = OperationContext.Current.GetCallbackChannel<IMainServiceCallback>();
                    }

                }
            }
        }

        /// <summary>
        /// 移除用户，如果用户太久没有活动
        /// </summary>
        /// <param name="serverData"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static bool RemoveClientIfTimeout(ServerData serverData, Client client)
        {
            if (client != null)
            {
                if (DateTime.Now.Ticks - client.LatestActiveTicks >= s_timeoutTicks)
                {
                    if (client.Domain != null)
                    {
                        ControlDomain domain = client.Domain;
                        domain.RemoveClient(client);

                        if (!domain.HasAnyClient())
                        {
                            serverData.DeleteDomain(domain);
                        }
                    }
                    else
                    {
                        Logger.Warning("client:{0},的domain为空-RemoveClientIfTimeout", client.UserIdentity);
                    }
                    serverData.RemoveClient(client);
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 移除超时的用户，但是保留控制域
        /// </summary>
        /// <param name="serverData"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public static bool RemoveClientIfTimeoutButReserveDomain(ServerData serverData, Client client)
        {
            if (client != null)
            {
                if (DateTime.Now.Ticks - client.LatestActiveTicks >= s_timeoutTicks)
                {
                    if (client.Domain != null)
                    {
                        ControlDomain domain = client.Domain;
                        domain.RemoveClient(client);
                    }
                    else
                    {
                        Logger.Warning("client:{0},的domain为空-RemoveClientIfTimeoutButReserveDomain", client.UserIdentity);
                    }
                    serverData.RemoveClient(client);
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断终端是否存活超时了。
        /// </summary>
        /// <param name="client"></param>
        /// <returns>true:timeout, false:not timeout</returns>
        public static bool IsLiveTimeout(Client client)
        {
            if (client != null)
            {
                if (DateTime.Now.Ticks - client.LatestActiveTicks >= s_timeoutTicks)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
